package com.jh3502.vivo.function.todo;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh3502.vivo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TodoListAdapter extends RecyclerView.Adapter<TodoListAdapter.ViewHolder> implements View.OnClickListener {


    private Context mContext;
    private List<TodoListBean> dataList;

    private View convertView;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private ButtonInterface viewClickReject, viewClickConfirm;

    public TodoListAdapter(Context context, List<TodoListBean> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    public void setButtonSetOnclickReject(ButtonInterface buttonInterface) {
        this.viewClickReject = buttonInterface;
    }

    public void setButtonSetOnclickConfirm(ButtonInterface buttonInterface) {
        this.viewClickConfirm = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_todo, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        TodoListBean todoListBean = dataList.get(position);
        StringBuffer sbTitle = new StringBuffer();
        if (todoListBean.getType() == 0) {
            sbTitle.append("审批：").append(todoListBean.getName()).append("的").append("出差审批");
            Glide.with(mContext).load(R.drawable.point_purple).into(viewHolder.itemTodoPoint);
            Glide.with(mContext).load(R.drawable.ic_label_travel).into(viewHolder.itemTodoStatus);
        } else if (todoListBean.getType() == 1) {
            sbTitle.append("审批：").append(todoListBean.getName()).append("的").append("报销审批");
            Glide.with(mContext).load(R.drawable.point_blue).into(viewHolder.itemTodoPoint);
            Glide.with(mContext).load(R.drawable.ic_label_reimburse).into(viewHolder.itemTodoPoint);
        }
        viewHolder.itemTodoTitle.setText(sbTitle.toString());
        viewHolder.itemTodoNum.setText(todoListBean.getBusinessNo());
        if (todoListBean.getOvertop() == 0) {
            viewHolder.itemTodoOut.setText("否");
            viewHolder.itemTodoOut.setTextColor(Color.parseColor("#FF666666"));
        } else {
            viewHolder.itemTodoOut.setText("是");
            viewHolder.itemTodoOut.setTextColor(Color.parseColor("#FFF36C60"));
        }
        viewHolder.itemTodoDate.setText(todoListBean.getCreateDate());

        viewHolder.itemTodoConfirm.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickConfirm != null) {
                viewClickConfirm.onClick(position);
            }
        });
        viewHolder.itemTodoReject.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickReject != null) {
                viewClickReject.onClick(position);
            }
        });

    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int i);
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_todo_title)
        TextView itemTodoTitle;
        @BindView(R.id.item_todo_status)
        ImageView itemTodoStatus;
        @BindView(R.id.item_todo_num)
        TextView itemTodoNum;
        @BindView(R.id.item_todo_out)
        TextView itemTodoOut;
        @BindView(R.id.item_todo_date)
        TextView itemTodoDate;
        @BindView(R.id.item_todo_reject)
        Button itemTodoReject;
        @BindView(R.id.item_todo_confirm)
        Button itemTodoConfirm;
        @BindView(R.id.item_todo_point)
        ImageView itemTodoPoint;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}